<?php
namespace app\services;

use app\services\ExcelService;
use app\utils\Functions as F;

class TanxManageService extends BaseService {

	private $matrix = [
		'status' => 'commit_status',
		'name' => 'name',
		'id' => 'advertiser_id',
		'type' => 'type',
		'content' => 'content',
		'submitDate' => 'submit_date',
		'expireDate' => 'expire_date',
		'effectStatus' => 'effect_status',
	];

	private $importMatrix = [
		'commit_status' => '审核状态',
		'name' => '客户名称',
		'advertiser_id' => ['客户ID', 'return (string)$v;'],
		'type' => ['客户类型','return (string)$v;'],
		'content' => '资质模板名称',
		'submit_date' => '资质提交时间',
		'expire_date' => '资质到期时间',
		'effect_status' => '生效状态',
	];

	public function getTanxAdsList() {
		$data = $this->getAll('AdvertiserMap', ['ad_network_id' => 2]);
		$data = $this->fieldsMap($this->matrix, $data);
		return $data;
	}

	public function getImportData() {
		$fileInfo = $this->uploadExcel();
		return $this->addImportData($fileInfo);
	}

	public function addImportData($fileInfo) {
        if (is_array($fileInfo)) {
        	$fileInfo = $fileInfo[0];
        	$serv = new ExcelService();
            $datas = $serv->readXlsx($fileInfo['savepath'].$fileInfo['savename']); 
            $datas = $this->fieldsMap($this->importMatrix, $datas);
            foreach ($datas as $data) {
            	$uniqueKey['advertiser_id'] = $data['advertiser_id'];
            	$data['vam_advertiser_id'] = $data['advertiser_id'];
            	$data['ad_network_id'] = 2;
            	$this->saveOn('AdvertiserMap', $uniqueKey, $data);
            }
            $fileInfo['size'] = number_format($fileInfo['size']/1024, 2).'k';
        }
        return $fileInfo;		
	}

	public function getRef($result) {
        if (is_array($result)) {
            $table[] = [
                'name' => $result['name'],
                'size' => $result['size'],
                'date' => date('Y/m/d H:i:s'),
                'judgeInfo' => '符合需求',
                'updater' =>  F::get_client_ip(),
            ];
        }
        return ['table' => $table];
    }

	
}